#include "PrecompiledHeader.h"
#include "CtrlCheckbox.h"

/*--------------------------------------------------------------------------*/

CtrlCheckbox::CtrlCheckbox(ICtrl* parent, const std::string& name)
	: ICtrl(parent, name)
{
	this->m_var = NULL;
}

/*--------------------------------------------------------------------------*/

CtrlCheckbox::~CtrlCheckbox()
{

}

/*--------------------------------------------------------------------------*/

void CtrlCheckbox::Draw()
{
	if (!this->m_var)
	{
		this->SetVar(g_Shell->GetVar(this->m_varName));
	}

	if (this->GetFont())
	{
// 		OpenGL::DrawRectangle(this->m_screenPosition,
// 		                      this->m_screenPosition + glm::vec2(this->m_font->GetSize(), this->m_font->GetSize()),
// 		                      this->m_background);

		g_Renderer->DrawText2D(this->GetFont(),
		                       this->m_label,
		                       this->m_screenPosition + glm::vec2(this->m_font->GetSize(), 0.0f),
		                       this->m_fontColor);
	}

	if (this->m_var)
	{
		if (this->m_var->GetBool())
		{
// 			OpenGL::DrawRectangle(this->m_screenPosition + glm::vec2(2.0f, 2.0f),
// 			                      this->m_screenPosition + glm::vec2(this->m_font->GetSize() - 2.0f, this->m_font->GetSize() - 2.0f),
// 			                      this->m_tickColor);
		}
	}


//	glm::vec2 aa, bb;

// 	cr->DrawRectangle(scrpos, scrpos + glm::vec2(this->GetSize().y, this->GetSize().y), gf->GetSecondaryBackgroundColor());
//
// 	if (this->status_IsLClick)
// 	{
// 		aa = scrpos + glm::vec2(1.0f, 1.0f);
// 		bb = scrpos + this->GetSize() + glm::vec2(1.0f, 1.0f);
//
// 		cr->DrawRectangle(aa, bb, gf->GetPrimaryBackgroundColor());
// 	}
// 	else if (this->status_IsCMouseOver == false)
// 	{
// 		aa = scrpos + glm::vec2(1.0f, 1.0f);
// 		bb = scrpos + this->GetSize() - glm::vec2(1.0f, 1.0f);
//
// 		cr->DrawRectangle(aa, bb, gf->GetSecondaryBackgroundColor());
// 	}
// 	else
// 	{
// 		aa = scrpos + glm::vec2(1.0f, 1.0f);
// 		bb = scrpos + this->GetSize() - glm::vec2(1.0f, 1.0f);
//
// 		cr->DrawRectangle(aa, bb, gf->GetButtonColor());
// 	}
//
// 	if (this->m_var && this->m_var->GetInt())
// 	{
// 		aa = scrpos + glm::vec2(5.0f, 5.0f);
// 		bb = scrpos + this->GetSize() - glm::vec2(5.0f, 5.0f);
//
// 		cr->DrawRectangle(aa, bb, gf->GetTextColor());
// 	}
//
// 	glm::vec2 textmargin;
//
// 	textmargin.x = this->GetSize().x;
// 	textmargin.y = (this->GetSize().y - this->GetFont()->GetSize());
//
// 	aa = scrpos + textmargin;
// 	cr->PutText(this->GetLabel(), aa, gf->GetTextColor(), this->GetFont());

	this->CallChildsDraw();
}

/*--------------------------------------------------------------------------*/

void CtrlCheckbox::SetVar(Variable* var)
{
	//LogE(!var) << "Binding NULL pointer to checkbox";
	this->m_var = var;
}

/*--------------------------------------------------------------------------*/

Variable* CtrlCheckbox::GetVar() const
{
	return this->m_var;
}

/*--------------------------------------------------------------------------*/

void CtrlCheckbox::ReadSpecials(std::ifstream& file, const std::string& firstWord)
{
	if (firstWord == "var")
	{
		file >> this->m_varName;
	}
	else if (firstWord == "tickcolor")
	{
		uint r, g, b, a;
		file >> r >> g >> b >> a;

		this->m_tickColor = Color(r, g, b, a);
	}
	else
	{
		this->ReadBasicGfx(file, firstWord);
		this->ReadTextSupport(file, firstWord);
	}
}

/*--------------------------------------------------------------------------*/

void CtrlCheckbox::OnLMBClick(const glm::vec2& clickPosition)
{
	if (this->m_var)
	{
		if (this->m_var->GetBool())
		{
			this->m_var->Set(false);
		}
		else
		{
			this->m_var->Set(true);
		}
	}
}

/*--------------------------------------------------------------------------*/

void CtrlCheckbox::OnRMBClick(const glm::vec2& clickPosition)
{
	this->OnLMBClick(clickPosition);
}


/*--------------------------------------------------------------------------*/